/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

/**
 * Create a new page that let an author manage a survey
 */
qx.Class.define("bip.authorview.pages.ManageSurveyPage", 
{
	extend: bip.views.Page,

	/*
	*****************************************************************************
	 EVENTS
	*****************************************************************************
	*/  
	events :
	{
		
		
	},

	/*
	*****************************************************************************
	 PROPERTIES
	*****************************************************************************
	*/ 
	properties: {
		
  	},

	/*
	*****************************************************************************
	 CONSTRUCTOR
	*****************************************************************************
	*/
	
	/**
	 * Construct the window
	 */	
 	construct: function(pageModel)
	{
		this.base(arguments,pageModel,this.tr("Manage survey"),this.tr("Manage survey"),null,"icon/48/apps/utilities-text-editor.png");
		
		var service = bip.services.Survey.getInstance();
		service.addListener("error",function(evt) {
			this.setStateWithDescription("error",evt.getData().getMessage());
		},this);
		service.addListener("loaded",function(evt) {
			this.__uiManager.reset();
			this.__treeController.setModel(evt.getData());
			this.__uiManager.setSurveyFormModel(evt.getData());
			this.setStateWithDescription("ready","Ready");
		},this);
		service.addListener("loading",function(evt) {
			this.setStateWithDescription("running",evt.getData()+","+ this.tr("please wait..."));
		},this);
		
		qx.locale.Manager.getInstance().addListener("changeLocale", function(evt) {
			bip.services.Survey.getInstance().getSurveyTree();
		}, this);
		
		bip.services.Survey.getInstance().getSurveyTree();
	},

	/*
	*****************************************************************************
	 MEMBERS
	*****************************************************************************
	*/	
	members:
	{	
		__model: null,
		__form: null,
		__uiManager: null,
		__treeController: null,
		
		/**
     * Create the content of the dialog. 
     * Extending classes must implement this method.
     */
    _createWidgetContent : function()
    {
    	this.__createActionBar(this.getChildControl("actionbar"));
    	this.setLayout(new qx.ui.layout.VBox(5));
    	this.__uiManager = new bip.authorview.ui.SurveyManagerView();
			this.add(this.__uiManager,{flex:1});
      
			// Tree properites
			this.__uiManager.getChildControl("tree").set({openMode:"dblclick"});
			
      // create the controller
      this.__treeController = new qx.data.controller.Tree(null, this.__uiManager.getChildControl("tree"), "items", "name");
      this.__treeController.setDelegate({
  	    
  			bindItem : function(controller, item, id) {
  				//item.setModel(controller.getModel().getItem(id));
  				//qx.log.Logger.debug(bip.authorview.pages.ManageSurveyPage,qx.dev.Debug.debugProperties(item.getModel()));
  				controller.bindProperty("name", "label", null, item, id);
  				
  				if (item.getModel()["getTopic"]) {
  					controller.bindProperty("topic.idLocal", "icon", {
  	  	      converter: function(data) {
  	  	      	return "bip/flags/"+data+".png";
  	  	      }
  	  	     }, item, id);
  				}
  				
  				if (item.getModel()["getQuestion"]) {
  					controller.bindProperty("question.idLocal", "icon", {
  	  	      converter: function(data) {
  	  	      	return "bip/icon/question.png";
  	  	      }
  	  	     }, item, id);
  				}
  				
//  				controller.bindProperty("idLocal", "icon", null, item, id);
  			}
  		});
      this.__treeController.addListener("changeSelection",function(evt) {
      	var model = evt.getData();
      	if (model.length > 0) {
      		var firstItem = model.getItem(0);
      		if (firstItem["getTopic"]) {
      			this.__uiManager.setTopicFormModel(firstItem);
      		} 
      		else if (firstItem["getQuestion"]) {
      			this.__uiManager.setQuestionFormModel(firstItem);
      		}  else {
      			this.__uiManager.setSurveyFormModel(firstItem);
      		}
      	}
      },this);
      
    },
    
    /**
		 * Fill up the controler with the button bar
		 */
		__createActionBar: function(control) {
			var reloadCmd = new qx.ui.core.Command();
			reloadCmd.addListener("execute",this.__onReloadButtonClickHandler,this);
			this.populateActionBar({
				buttons:[
							{id:"reload", type:"RefreshButton", label: "Reload",command:reloadCmd}
						]
			},control);
			//this.setActionBarButtonsProperties({width:80,height:20});
		},
		
		__onReloadButtonClickHandler: function(evt) {
			bip.services.Survey.getInstance().getSurveyTree();
		},
		
		_beforeShow: function() {
			//this.setStateWithDescription("ready",this.tr("Use this form to fill a new survey"));
			//this.maximize();
	    }
		
	    

		/*
	  	*****************************************************************************
	     UI
	  	*****************************************************************************
	 	*/
		

	},
	/*
	 * ****************************************************************************
	 * DESTRUCTOR
	 * ****************************************************************************
	 */

	destruct : function() {
		this._disposeObjects("__model","__form","__uiManager","__treeController");
		
	}
	
});

